﻿#ifndef FRAMELESSWINDOW_H
#define FRAMELESSWINDOW_H

#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QMouseEvent>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QStyle>

class FramelessWindow : public QWidget
{
    Q_OBJECT

public:
    explicit FramelessWindow(QWidget* parent = nullptr);

    void setIcon(const QString& icon);
    void setTitle(const QString& title);
    void setHeadBackgroundColor(const QString& color);

    void setContentWidget(QWidget* widget);

    void hideMaximizeButton(bool hidden = true);
    void hideMinimizeButton(bool hidden = true);
    void hideCloseButton(bool hidden = true);
    void hideIcon(bool hidden = true);
    void hideTitle(bool hidden = true);

protected:
    void mousePressEvent(QMouseEvent* event) override;
    void mouseReleaseEvent(QMouseEvent* event) override;
    void mouseDoubleClickEvent(QMouseEvent* event) override;
    void mouseMoveEvent(QMouseEvent* event) override;

private slots:
    void minimizeWindow();
    void toggleMaximize();
    void closeWindow();

private:
    enum ResizeEdge
    {
        None,
        Top,
        TopRight,
        Right,
        BottomRight,
        Bottom,
        BottomLeft,
        Left,
        TopLeft
    };

    void setupUi();
    void setupWindowControls();
    bool isInTitleBarArea(const QPointF& pos);
    bool isInResizeArea(const QPointF& pos);
    void updateCursorShape(const QPointF& pos);

    // 成员变量
    QVBoxLayout* m_layout = nullptr;
    QWidget* m_main_widget = nullptr;

    QPoint m_dragPosition;
    bool m_dragging = false;
    bool m_resizing = false;
    QPoint m_resizeStartPos;
    QRect m_resizeStartGeometry;
    ResizeEdge m_resizeEdge = None;

    int MarginSize = 10;

    /*************************** head widget ***************************/
    QWidget* m_head_widget = nullptr;
    QLabel* m_icon_label = nullptr;
    QLabel* m_title_label = nullptr;
    QPushButton* m_min_button = nullptr;
    QPushButton* m_max_button = nullptr;
    QPushButton* m_close_button = nullptr;
    /*******************************************************************/

    /*************************** body widget ***************************/
    QWidget* m_body_widget = nullptr;
    QHBoxLayout* m_body_layout = nullptr;
    /*******************************************************************/

    /*************************** foot widget ***************************/
    QWidget* m_foot_widget = nullptr;
    /*******************************************************************/
};

#endif // FRAMELESSWINDOW_H
